package com.mhxy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mhxy.constant.PicConstants;
import com.mhxy.constant.TaskConstants;
import com.mhxy.entity.GameLoginInfo;
import com.mhxy.handler.ConfigHandler;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;

@Slf4j
public class SwitchAccountTaskImpl extends AbstractTask {

    private String hwndName;

    private GameLoginInfo gameLoginInfo;

    public SwitchAccountTaskImpl(WinDef.HWND hwnd_0, String hwndName) {
        super(hwnd_0);
        this.hwndName = hwndName;
    }

    @Override
    public Boolean before() throws Exception {
        String point = StrUtil.subAfter(hwndName, "_", true);
        if (!ConfigHandler.checkCurrentLoginUser(point)) {
            log.info("检测到当前窗口未配置登录角色");
            TaskProgressHandler.updateProgress(getTaskName(), "未配置角色");
            return false;
        }

        clickEsc3();
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_下面的活动开关, 1)) {
            return true;
        }
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {

        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_设置)) {
            log.error("【{}】没有找到“设置”按钮！", getTaskName());
            return false;
        }
        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_切换账号)) {
            log.error("【{}】没有找到“切换账号”按钮！", getTaskName());
            return false;
        }
        ThreadUtil.sleep1();
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_登出)) {
            log.error("【{}】没有找到“登出”按钮！", getTaskName());
            return false;
        }

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        ThreadUtil.sleep(5000);

        // 添加“登录账号”任务
        TaskProgressHandler.addTasks(hwndName, Collections.singletonList(TaskConstants.LOGIN_GAME));
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "切换登录账号";
    }
}
